<?php

class ProfileAction extends BaseAction {

    public function register() {
        $this->assign("title", "去玩吗注册");
        $this->assign("description", "欢迎你注册去玩吗网站");
        
        $this->display();
    }

    public function doregister() {
        $Member = D("Member");
        if ($Member->Create()) {
            $email = $_POST['email'];
            $Member->password = md5($_POST['password']);
            $name = explode("@", $email);
            $Member->username = $name[0];
            $Member->lastlogintime = time();
            $memberid = $Member->add();

            if ($memberid > 0) {
                
                $template = D("Template")->getByCode('verify');
                $mail['to'] = $email;
                $mail['subject'] = $template['subject'];
                $identifying = mt_rand(10000, 99999);
                $url = $_SERVER['SERVER_NAME'] . '/index.php?m=Profile&a=verifyemail&id=' . $memberid . '&identifying=' . $identifying;
                $content = str_replace('$url', $url, $template['content']);
                $mail['content'] = $content;
                $this->sendMail($mail);
                
                $user = D("Member");
                $user->id = $memberid;
                $user->identifying = $identifying;
                $user->save();

                $checkUser = D("Member")->getById($memberid);

                Session::set(C('USER_AUTH_KEY'), $checkUser['id']);
                Session::set('member', $checkUser['email']);
                Session::set('lastlogintime', time());

                $this->redirect(__APP__ . '/membercenter/');
            } else {
                $this->assign("jumpUrl", "__URL__");
                $this->error("添加失败！");
            }
        } else {
            $this->error($Member->getError());
        }
    }

    public function verifyemail() {
        $id = $_GET['id'];
        $identifying = $_GET['identifying'];
        $Member = D("Member")->getByid($id);
        if ($Member['identifying'] == $identifying) {
            $user = D("Member");
            $user->id = $Member['id'];
            $user->identifying = '';
            $user->save();
            $this->success("验证成功！");
        } else {
            $this->error("验证失败，请重新发邮件！");
        }
    }

    public function login() {
        $this->assign("title", "登录");
        $this->assign("description", "寻找附近共同运动爱好的人一起去玩");
        
        $this->display();
    }

    public function ajaxlogin() {
        $this->display("Public:ajaxlogin");
    }

    public function dologin() {
        if ($_SESSION['verify'] != md5($_POST['verify'])) {
            $this->error('验证码错误！');
        } else {
            $Member = D("Member");
            $map['email'] = $_POST['email'];
            $map['password'] = md5($_POST['password']);
            $checkUser = $Member->where($map)->find();
            if (!$checkUser) {
                $this->assign("jumpUrl", "__APP__");
                $this->error("用户名或密码不正确！");
            } else {
                Session::set(C('USER_AUTH_KEY'), $checkUser['id']);
                Session::set('member', $checkUser['email']);
                Session::set('lastlogintime', $checkUser['lastlogintime']);
                $Member->where("id = " . $checkUser['id'])->setField("lastlogintime", time());

                $this->redirect(__APP__ . '/membercenter/');
            }
        }
    }

    public function logout() {
        if (Session::is_set(C('USER_AUTH_KEY'))) {
            Session::clear();
        }
        $this->redirect(__URL__ . '/login');
        $this->forward();
    }

    public function findpassword() {
        $this->display();
    }

    public function dofind() {
        if ($_SESSION['verify'] != md5($_POST['verify'])) {
            $this->error('验证码错误！');
        }
        $email = $_POST['email'];
        $user = D("Member")->getByEmail($email);
        if (!empty($user)) {
            $template = D("Templates")->getByCode('findpsw');

            $mail = D("Mail");
            $mail->Create();
            $mail->to = $email;
            $mail->subject = $template['subject'];
            $identifying = build_str_rand(6, 0);
            $url = $_SERVER['SERVER_NAME'] . '/index.php?m=Profile&a=changepsw&id=' . $user['id'] . '&identifying=' . $identifying;
            $content = str_replace('$url', $url, $template['content']);
            $mail->content = $content;

            $mail->add();
            $Member = D("Member");
            $Member->id = $user['id'];
            $Member->identifying = $identifying;
            $Member->save();

            $this->assign("jumpUrl", "__APP__/profile/login");
            $this->success("添加成功！");
        } else {
            $this->assign("jumpUrl", "__URL__");
            $this->error("添加失败！");
        }
    }

    public function changepsw() {
        $id = $_GET['id'];
        $identifying = $_GET['identifying'];
        $Member = D("Member")->getById($id);
        if ($Member['identifying'] == $identifying) {
            //$this->assign("id", $id)->assign("identifying", $identifying);
            $this->assign("id", $id);
            $this->assign("identifying", $identifying);
            $this->display("Public:changepsw");
        } else {
            $this->error("验证失败，请重新发邮件！");
        }
    }

    public function dochangepsw() {
        $id = $_POST['id'];
        $password = $_POST['password'];
        $repassword = $_POST['repassword'];
        $identifying = $_POST['identifying'];
        $Member = D("Member")->getById($id);
        if ($Member['identifying'] != $identifying) {
            $this->error("验证失败，请重新发邮件！");
        }
        if ($password != $repassword) {
            $this->error("验证失败，请重新发邮件！");
        }


        if ($password == $repassword) {
            $Member = D("Member");
            $Member->id = $id;
            $Member->password = md5($password);
            $Member->identifying = '';
            $Member->save();
            $this->assign("jumpUrl", "__APP__/profile/login");
            $this->success("添加成功！");
        } else {
            $this->error("验证失败，请重新发邮件！");
        }
    }

    public function verify() {
        $type = isset($_GET['type']) ? $_GET['type'] : 'gif';
        import("@.ORG.Image");
        Image::buildImageVerify();
        //Image::buildImageVerify(4, 3, $type, '', '20px');
    }

}

?>